package com.xiaomi.baidumap.hotuser;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaomi.baidumap.R;
import com.xiaomi.baidumap.common.LogUtils;
import com.xiaomi.baidumap.model.User;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 16-8-31.
 */
public class HotUserAdapter extends BaseAdapter {

    private List<User> lists;


    public HotUserAdapter() {
        lists = new ArrayList<>();
    }
    //添加数据
    public void addAll(List<User> users){
        lists.addAll(users);
        notifyDataSetChanged();
    }

    //清除数据
    public void clean(){
        lists.clear();
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return lists == null ? 0:lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_user,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        User user = lists.get(position);
        LogUtils.d("8888");
        LogUtils.d(user.getLogin()+"\\\\");
        holder.tvLoginName.setText(user.getLogin());
        //图片加载
        ImageLoader.getInstance().displayImage(user.getAvatar(),holder.ivIcon);
        return convertView;
    }

    static class ViewHolder{
       @BindView(R.id.ivIcon)
        ImageView ivIcon;
        @BindView(R.id.tvLoginName)
        TextView tvLoginName;


        ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
